/**
 * Project Name:b2c-platform
 * File Name:MailUtils.java
 * Package Name:cn.bluemobi.platform.utils
 * Date:2016年5月12日下午5:53:29
 * Copyright (c) 2016, bluemobi.cn All Rights Reserved.
 *
*/

package cn.bluemobi.platform.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import cn.bluemobi.platform.core.utils.PropertiesUtils;

/**
 * Description: <br/>
 * Date: 2016年5月12日 下午5:53:29 <br/>
 * 
 * @author hut
 * @version
 * @see
 */
public class MailUtils {

    private static final String SOURCE_EMAIL = PropertiesUtils.getProperty("EMAIL_SRC");

    private static final String SOURCE_PSD = PropertiesUtils.getProperty("EMAIL_PASSWORD");

    private static final String SERVER = PropertiesUtils.getProperty("EMAIL_SERVER");

    /**
     * @param title 邮件标题
     * @param content 邮件内容
     * @param to 收件人地址
     * @param copy 抄送人地址
     */
    public static boolean sendMail(String title, String content, String to, String... copy) {
        // 配置发送邮件的环境属性
        final Properties props = new Properties();
        /*
         * 可用的属性： mail.store.protocol / mail.transport.protocol / mail.host /
         * mail.user / mail.from
         */
        // 表示SMTP发送邮件，需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", SERVER);
        // 发件人的账号
        props.put("mail.user", SOURCE_EMAIL);
        // 访问SMTP服务时需要提供的密码
        props.put("mail.password", SOURCE_PSD);

        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        // 构建授权信息，用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        try {
            InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
            message.setFrom(form);
            // 设置收件人
            InternetAddress toc = new InternetAddress(to);
            message.setRecipient(RecipientType.TO, toc);
            // 设置抄送
            if (copy != null && copy.length > 0) {
                for (String cop : copy) {
                    InternetAddress cc = new InternetAddress(cop);
                    message.setRecipient(RecipientType.CC, cc);
                }
            }
            // 设置邮件标题
            message.setSubject(title);
            // 设置邮件的内容体
            message.setContent(content, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
            return false;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        sendMail("你好", "<html><body>你好啊</body></html>", "tao.hu@bluemobi.cn");
    }
}
